#include "bsp.h"
#include "touchscreen.h"
#include "bsp_ns2009.h"
#include "tscal.h"

struct ts_ns2009_pdata_t *ts_ns2009_pdata;


touchscreen_config_t touchscreen_config=
{
        .calibration[0] = -42,
        .calibration[1] = 5966,
        .calibration[2] = -2407088,
        .calibration[3] = 4278,
        .calibration[4] = -55,
        .calibration[5] = -4605674,
        .calibration[6] = 65536,
};
///////////////
int touchscreen_init(void* arg)
{
    touchscreen_config_t* config = (touchscreen_config_t*)arg;
    ts_ns2009_pdata = ts_ns2009_probe(config->calibration);
	
    return 0;
}

int touchscreen_read( int* type, int* x, int* y)
{
    ts_ns2009_poll(ts_ns2009_pdata);
    *x = 0;
    *y = 0;
    switch (ts_ns2009_pdata->event->type)
    {
        case TOUCH_BEGIN:
            *x = ts_ns2009_pdata->event->x;
            *y = ts_ns2009_pdata->event->y;
            *type = TOUCHSCREEN_STATUS_PRESS;
            break;

        case TOUCH_MOVE:
            *x = ts_ns2009_pdata->event->x;
            *y = ts_ns2009_pdata->event->y;
            *type = TOUCHSCREEN_STATUS_MOVE;
            break;

        case TOUCH_END:
            *x= ts_ns2009_pdata->event->x;
            *y= ts_ns2009_pdata->event->y;
            *type = TOUCHSCREEN_STATUS_RELEASE;
            break;
        default:
            break;
    }
    return 0;
}

int touchscreen_calibrate(int w, int h, int* cal)
{
    return do_tscal(ts_ns2009_pdata, w, h, cal);
}

